package com.example.myanimation;

import android.animation.ArgbEvaluator;
import android.animation.IntEvaluator;
import android.animation.ValueAnimator;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.Interpolator;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.example.myanimation.MyEvalutor.CharEvaluator;
import com.example.myanimation.MyEvalutor.myEvalutor;
import com.example.myanimation.MyInterpolator.MyInterpolator;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ValueActivity extends AppCompatActivity {


    @BindView(R.id.spinner)
    Spinner spinner;
    @BindView(R.id.bt)
    Button bt;
    @BindView(R.id.activity_value)
    LinearLayout activityValue;
    @BindView(R.id.pointView)
    MyPointView pointView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_value);
        ButterKnife.bind(this);
        initEvent();
    }

    private void initEvent() {
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 1:
                        myInterpolator(new BounceInterpolator(), ValueAnimator.ofInt(200, 600));
                        break;
                    case 2:
                        myInterpolator(new MyInterpolator(), ValueAnimator.ofInt(200, 600));
                        break;
                    case 3:
                        MyIntEvalutor();
                        break;
                    case 4:
                        MyEvalutor();
                        break;
                    case 5:
                        MyArgbEvalutor();
                        break;
                    case 6:
                        ofObjectTest();
                        break;
                    case 7:
                        pointView.doAnimation();
                        break;
                }
            }


            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

    private void ofObjectTest() {
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new CharEvaluator(), new Character('A'), new Character('Z'));
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Character chara = (Character) animation.getAnimatedValue();
                bt.setText(String.valueOf(chara));
            }
        });
        valueAnimator.setDuration(5000);
        valueAnimator.setInterpolator(new AccelerateInterpolator());
        valueAnimator.start();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void MyArgbEvalutor() {
        // 如果是ofInt,默认的Evalutor就是IntEvaluetor，如果是ofFloat 默认的就是FloatEvaluetor
        ValueAnimator valueAnimator = ValueAnimator.ofArgb(0xffffff00, 0xff0000ff);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int curValue = (int) animation.getAnimatedValue();
                bt.setBackgroundColor(curValue);
            }
        });
        valueAnimator.setDuration(1000);
        valueAnimator.setEvaluator(new ArgbEvaluator());
        valueAnimator.start();
    }

    private void MyEvalutor() {
        // 如果是ofInt,默认的Evalutor就是IntEvaluetor，如果是ofFloat 默认的就是FloatEvaluetor
        ValueAnimator valueAnimator = ValueAnimator.ofInt(200, 600);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 公式:curValue = 200+（600 - 200）*显示进度（是Interpolator接口中getInterpolation的方法）
                // 获取到的值其实就叫做Evaluator Evaluator其实就是一个转换器，他能把小数进度转换成对应的数值位置
                int curValue = (int) animation.getAnimatedValue();
                bt.layout(bt.getLeft(), curValue, bt.getRight(), curValue + bt.getHeight());
            }
        });
        valueAnimator.setDuration(1000);
        valueAnimator.setEvaluator(new myEvalutor());
        valueAnimator.setInterpolator(new BounceInterpolator());
        valueAnimator.start();
    }

    private void MyIntEvalutor() {
        // 如果是ofInt,默认的Evalutor就是IntEvaluetor，如果是ofFloat 默认的就是FloatEvaluetor
        ValueAnimator valueAnimator = ValueAnimator.ofInt(200, 600);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 公式:curValue = 200+（600 - 200）*显示进度（是Interpolator接口中getInterpolation的方法）
                // 获取到的值其实就叫做Evaluator Evaluator其实就是一个转换器，他能把小数进度转换成对应的数值位置
                int curValue = (int) animation.getAnimatedValue();
                bt.layout(bt.getLeft(), curValue, bt.getRight(), curValue + bt.getHeight());
            }
        });
        valueAnimator.setDuration(1000);
        valueAnimator.setEvaluator(new IntEvaluator());
        valueAnimator.setInterpolator(new MyInterpolator());
        valueAnimator.start();
    }


    public void myInterpolator(Interpolator interpolator, ValueAnimator valueAnimator) {
        // 2.添加监听
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                // 公式:curValue = 200+（600 - 200）*显示进度（是Interpolator接口中getInterpolation的方法）
                // 获取到的值其实就叫做Evaluator Evaluator其实就是一个转换器，他能把小数进度转换成对应的数值位置
                int curValue = (int) animation.getAnimatedValue();
                bt.layout(bt.getLeft(), curValue, bt.getRight(), curValue + bt.getHeight());
            }
        });
        // 设置动画的持续时间
        valueAnimator.setDuration(3000);
        // 设置动画的插值器
        valueAnimator.setInterpolator(interpolator);
        // 开始动画
        valueAnimator.start();
    }

}
